import pymysql
import requests
import execjs


class Mineral:
    def __init__(self):
        self.conn = pymysql.connect(host='192.168.80.3', port=3306, user='root', password='123456',
                                    database='py_spider')
        self.cursor = self.conn.cursor()
        self.headers = {
            "Accept": "application/json, text/plain, */*",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Content-Length": "0",
            "Origin": "https://ec.minmetals.com.cn",
            "Referer": "https://ec.minmetals.com.cn/open/home/purchase-info",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36",
            "sec-ch-ua": "^\\^Not.A/Brand^^;v=^\\^8^^, ^\\^Chromium^^;v=^\\^114^^, ^\\^Google",
            "sec-ch-ua-mobile": "?0",
            "sec-ch-ua-platform": "^\\^Windows^^"
        }
        self.create_table()

    def create_table(self):
        sql = """
            create table if not exists mineral(
                id int primary key auto_increment,
                report_name varchar(100) not null,
                lxmc varchar(50),
                company_name varchar(50),
                report_time datetime DEFAULT NULL
            );
        """
        try:
            self.cursor.execute(sql)
            print('数据表创建成功...')
        except Exception as e:
            print('数据表创建失败: ', e)

    def insert_info(self, *args):
        sql = """
            insert into mineral(
                report_name,
                lxmc,
                company_name,
                report_time
            ) values (%s, %s, %s, %s);
        """

        try:
            self.cursor.execute(sql, args)
            self.conn.commit()
            print('数据插入成功...')
        except Exception as e:
            print('数据插入失败: ', e)
            self.conn.rollback()

    def get_data(self, page):
        url = "https://ec.minmetals.com.cn/open/homepage/public"
        response = requests.post(url, headers=self.headers)
        pubkey = response.text

        # 获取返回结果
        data = {
            "inviteMethod": "",
            "businessClassfication": "",
            "mc": "",
            "lx": "ZBGG",
            "dwmc": "",
            "pageIndex": page
        }

        ctll = execjs.compile(open('五矿.js', encoding='utf-8').read())
        param = ctll.call('get_data', data, pubkey)
        post_data = {
            'param': param
        }
        data_url = 'https://ec.minmetals.com.cn/open/homepage/zbs/by-lx-page'
        response = requests.post(data_url, headers=self.headers, json=post_data)
        result = response.json()
        for data in result['list']:
            name = data['mc']
            lxmc = data['lxmc']
            company = data['dwmc']
            time = data['rq']
            print("入库成功：", name)
            # self.insert_info(name, lxmc, company, time)


if __name__ == '__main__':
    m = Mineral()
    for page in range(1):
        m.get_data(page)
